import React from 'react';
import styles from './index.module.less';
import { Button } from 'antd';
import { useDispatch, useSelector } from 'react-redux';
import {
  get_topic_two_list,
  select_active_two,
  select_topic_two_list,
} from '@/redux/slice/subject';
import axios from '@/utils/http';

const SubjectList: React.FC = () => {
  const topic_two_list: any = useSelector(select_topic_two_list);
  const two_obj: any = useSelector(select_active_two);
  const dispatch = useDispatch();

  //删除题目列表
  const del_btn: any = async (id: string) => {
    await axios.delete(`/api/topic/${id}`);
    dispatch(get_topic_two_list(two_obj.value));
  };
  return (
    <div className={styles.wrap}>
      {topic_two_list?.map((item: any) => {
        return (
          <div className={styles.item} key={item._id}>
            <h2 className={styles.title}>{item.title}</h2>
            <Button
              type="primary"
              onClick={() => {
                del_btn(item._id);
              }}
            >
              删除
            </Button>
          </div>
        );
      })}
    </div>
  );
};

export default SubjectList;
